#include "Menu.h"

void Menu()
  {
    OptMenuBk();
    while (true)
      {
        OptItems();
        Mouse();
        FlushBatchDraw();
      }
  }

void Mouse()
  {
    MOUSEMSG mouse;
    mouse = GetMouseMsg();
    SelectItem(mouse);
  }
void OptItems()
  {
    OptNewButton();
    if (IsPlayed())
      OptContinueButton();
    OptHelpButton();
    OptExitButton();
  }
void OptMenuBk()
  {
    IMAGE ImgMenuBk;
    loadimage(&ImgMenuBk, _T("D://Pic/MenuBk.png"));
    TransparentImage(NULL, 0, 0, &ImgMenuBk);
  }
void OptNewButton()
  {
    int Y = 0;
    if (IsPlayed())
      Y = 100;
    else
      Y = 100;
    IMAGE NewImg;
    loadimage(&NewImg, _T("D://Pic/New.png"));
    TransparentImage(NULL, 640 - 151, Y, &NewImg);
  }
void OptContinueButton()
  {
    int Y = 257;
    IMAGE ContinueImg;
    loadimage(&ContinueImg, _T("D://Pic/Continue.png"));
    TransparentImage(NULL, 640 - 200, Y, &ContinueImg);
  }
void OptHelpButton()
  {
    int Y = 0;
    if (IsPlayed())
      Y = 414;
    else
      Y = 307;
    IMAGE HelpImg;
    loadimage(&HelpImg, _T("D://Pic/Help.png"));
    TransparentImage(NULL, 640 - 100, Y, &HelpImg);
  }
void OptExitButton()
  {
    int Y = 0;
    if (IsPlayed())
      Y = 572;
    else
      Y = 515;
    IMAGE ExitImg;
    loadimage(&ExitImg, _T("D://Pic/Exit.png"));
    TransparentImage(NULL, 640 - 202, Y, &ExitImg);
  }

void SelectItem(MOUSEMSG mouse)
  {
    SelectNew(mouse);
    if (IsPlayed())
      SelectContinue(mouse);
    SelectHelp(mouse);
    SelectExit(mouse);
  }

void SelectNew(MOUSEMSG mouse)
  {
    int InNewScope = 0;
    int static NewAlerted = 0;
    IsInNewScope(mouse, &InNewScope);
    OutNewScope(&InNewScope, &NewAlerted);
    LightNew(InNewScope, &NewAlerted);
    NewAlert(&NewAlerted);
    PressNewButton(mouse, InNewScope);
  }
void IsInNewScope(MOUSEMSG mouse, int *InNewScope)
  {
    int X = mouse.x, Y = mouse.y;
    int DepX = 640 - 151, DepY = 100;
    if ((X >= DepX) && (X <= DepX + 302))
      if ((Y >= DepY) && (Y <= DepY + 107))
        *InNewScope = 1;
  }
void OutNewScope(int *InNewScope, int *NewAlerted)
  {
    if (*InNewScope != 1)//Out of button
      *NewAlerted = 0;
  }
void LightNew(int InNewScope, int *NewAlerted)
  {
    int DepX = 640 - 151, DepY = 100;
    if (InNewScope)//Light
      {
        if (*NewAlerted == 0)
          *NewAlerted = 1;
        IMAGE NewSelected;
        loadimage(&NewSelected, _T("D://Pic/NewSelected.png"));
        TransparentImage(NULL, DepX, DepY, &NewSelected);
      }
  }
void NewAlert(int *NewAlerted)
  {
    if (*NewAlerted == 1)//Play sound
      {
        InScopeSound();
        *NewAlerted = 2;
      }
  }
void PressNewButton(MOUSEMSG mouse, int InNewScope)
  {
    if (InNewScope)
      if (mouse.uMsg == WM_LBUTTONDOWN)
        {
          PressNewSound();
          NewGame();
        }
  }

void SelectContinue(MOUSEMSG mouse)
  {
    int InContinueScope = 0;
    int static ContinueAlerted = 0;
    IsInContinueScope(mouse, &InContinueScope);
    OutContinueScope(InContinueScope, &ContinueAlerted);
    LightContinue(InContinueScope, &ContinueAlerted);
    ContinueAlert(&ContinueAlerted);
    PressContinueButton(mouse, InContinueScope);
  }
void IsInContinueScope(MOUSEMSG mouse, int *InContinueScope)
  {
    int X = mouse.x, Y = mouse.y;
    int DepX = 640 - 200, DepY = 257;
    if ((X >= DepX) && (X <= DepX + 401))
      if ((Y >= DepY) && (Y <= DepY + 107))
        *InContinueScope = 1;
  }
void OutContinueScope(int InContinueScope, int *ContinueAlerted)
  {
    if (InContinueScope != 1)//Out of buttom
      *ContinueAlerted = 0;
  }
void LightContinue(int InContinueScope, int *ContinueAlerted)
  {
    int DepX = 640 - 200, DepY = 257;
    if (InContinueScope)//Light
      {
        if (*ContinueAlerted == 0)
          *ContinueAlerted = 1;
        IMAGE ContinueSelected;
        loadimage(&ContinueSelected, _T("D://Pic/ContinueSelected.png"));
        TransparentImage(NULL, DepX, DepY, &ContinueSelected);
      }
  }
void ContinueAlert(int *ContinueAlerted)
  {
    if (*ContinueAlerted == 1)//Play sound
      {
        InScopeSound();
        *ContinueAlerted = 2;
      }
  }
void PressContinueButton(MOUSEMSG mouse, int InContinueScope)
  {
    if (InContinueScope)
      if (mouse.uMsg == WM_LBUTTONDOWN)
        {
          PressContinueSound();
        }
  }

void SelectHelp(MOUSEMSG mouse)
  {
    int InHelpScope = 0;
    int static HelpAlerted = 0;
    IsInHelpScope(mouse, &InHelpScope);
    OutHelpScope(InHelpScope, &HelpAlerted);
    LightHelp(InHelpScope, &HelpAlerted);
    HelpAlert(&HelpAlerted);
    PressHelpButton(mouse, InHelpScope);
  }
void IsInHelpScope(MOUSEMSG mouse, int *InHelpScope)
  {
    int DepY = 0;
    if (IsPlayed())
      DepY = 414;
    else
      DepY = 307;
    int X = mouse.x, Y = mouse.y;
    int DepX = 640 - 101;
    if ((X >= DepX) && (X <= DepX + 203))
      if ((Y >= DepY) && (Y <= DepY + 108))
        *InHelpScope = 1;
  }
void OutHelpScope(int InHelpScope, int *HelpAlerted)
  {
    if (InHelpScope != 1)//Out of button
      *HelpAlerted = 0;
  }
void LightHelp(int InHelpScope, int *HelpAlerted)
  {
    int DepY = 0;
    if (IsPlayed())
      DepY = 414;
    else
      DepY = 307;
    int DepX = 640 - 101;
    if (InHelpScope == 1)
        {
          if (*HelpAlerted == 0)
            *HelpAlerted = 1;
          IMAGE HelpSelected;
          loadimage(&HelpSelected, _T("D://Pic/HelpSelected.png"));
          TransparentImage(NULL, DepX, DepY, &HelpSelected);
        }//Light
  }
void HelpAlert(int *HelpAlerted)
  {
    if (*HelpAlerted == 1)
      {
        InScopeSound();
        *HelpAlerted = 2;
      }//reset sound
  }
void PressHelpButton(MOUSEMSG mouse, int InHelpScope)
  {
    if (InHelpScope)
      if (mouse.uMsg == WM_LBUTTONDOWN)
        {
          PressHelpSound();
        }
  }

void SelectExit(MOUSEMSG mouse)
  {
    int InExitScope = 0;
    int static ExitAlerted = 0;
    IsInExitScope(mouse, &InExitScope);
    OutExitScope(InExitScope, &ExitAlerted);
    LightExit(InExitScope, &ExitAlerted);
    ExitAlert(&ExitAlerted);
    PressExitButton(mouse, InExitScope);
  }
void IsInExitScope(MOUSEMSG mouse, int *InExitScope)
  {
    int DepY = 0;
    if (IsPlayed())
      DepY = 572;
    else
      DepY = 515;
    int X = mouse.x, Y = mouse.y;
    int DepX = 640 - 202;
    if ((X >= DepX) && (X <= DepX + 404))
      if ((Y >= DepY) && (Y <= DepY + 108))
        *InExitScope = 1;
  }
void OutExitScope(int InExitScope, int *ExitAlerted)
  {
    if (InExitScope != 1)//Out of buttom
      *ExitAlerted = 0;//reset sound
  }
void LightExit(int InExitScope, int *ExitAlerted)
  {
    int DepY = 0;
    if (IsPlayed())
      DepY = 572;
    else
      DepY = 515;
    int DepX = 640 - 202;
    if (InExitScope)//Light
        {
          if (*ExitAlerted == 0)
            *ExitAlerted = 1;
          IMAGE ExitSelected;
          loadimage(&ExitSelected, _T("D://Pic/ExitSelected.png"));
          TransparentImage(NULL, DepX, DepY, &ExitSelected);
        }
  }
void ExitAlert(int *ExitAlerted)
  {
    if (*ExitAlerted == 1)//Play sound
      {
        InScopeSound();
        *ExitAlerted = 2;
      }
  }
void PressExitButton(MOUSEMSG mouse, int InExitScope)
  {
    if (InExitScope)
      if (mouse.uMsg == WM_LBUTTONDOWN)
        {
          PressExitSound();
          Sleep(200);
          exit(0);
        }
  }

void TransparentImage(IMAGE *dstimg, int x, int y, IMAGE *srcimg)
  {
  	HDC dstDC = GetImageHDC(dstimg);
  	HDC srcDC = GetImageHDC(srcimg);
  	int w = srcimg->getwidth();
  	int h = srcimg->getheight();
  	BLENDFUNCTION bf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA};
  	AlphaBlend(dstDC, x, y, w, h, srcDC, 0, 0, w, h, bf);
  }
